package com.leadingsoft.core.config;

import javax.servlet.MultipartConfigElement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.leadingsoft.core.config.filter.LoginInterceptor;
import com.leadingsoft.web.core.service.CoreService;

@EnableWebMvc
@Configuration
public class KnowledgeAppConfig extends WebMvcConfigurerAdapter {
	@Autowired
	private CoreService coreService;

	@Bean
	LoginInterceptor loginInterceptor() {
		return new LoginInterceptor(coreService);
	}
	@Bean
	public MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory factory = new MultipartConfigFactory();
		// 置文件大小限制 ,超出此大小页面会抛出异常信息
		factory.setMaxFileSize("20MB"); // KB,MB
		// 设置总上传数据总大小
		factory.setMaxRequestSize("20MB");
		// 设置文件临时文件夹路径
		// factory.setLocation("E://test//");
		// 如果文件大于这个值，将以文件的形式存储，如果小于这个值文件将存储在内存中，默认为0
		// factory.setMaxRequestSize(0);
		return factory.createMultipartConfig();
	}
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 多个拦截器组成一个拦截器链
		// addPathPatterns 用于添加拦截规则
		// excludePathPatterns 用户排除拦截
		registry.addInterceptor(loginInterceptor()).excludePathPatterns("/w/download/*");
		registry.addInterceptor(loginInterceptor()).addPathPatterns("/**");
		super.addInterceptors(registry);
	}
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/views/**").addResourceLocations("classpath:/views/");
		registry.addResourceHandler("./**").addResourceLocations("classpath:/views/");
		super.addResourceHandlers(registry);
	}
}
